<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Goods;
use Carbon\Carbon;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class TopBookController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content
			->header('图书销售排行榜')
			->description('ISBN图书销售排行')
			->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content) {
		return $content
			->header('Detail')
			->description('description')
			->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content
			->header('Edit')
			->description('description')
			->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content
			->header('Create')
			->description('description')
			->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new Grid(new Goods);
		$grid->disableExport();
		$grid->disableCreateButton();
		$grid->disableActions();
		$grid->model()->selectRaw("*,count(isbn) as num")->groupBy('isbn')->orderBy('num', 'desc');

		$grid->id('ID');
		$grid->item_id('商品ID')->display(function ($item_id) {
			$url = $this->url;
			return "<a target='_blank'  href='" . $url . "'>" . $item_id . "</a>";
		});
		$grid->isbn('ISBN')->display(function ($isbn) {
			$url = route('goods.index') . '?isbn=' . $isbn;
			return "<a target='_blank'  href='" . $url . "'>" . $isbn . "</a>";
		});
		$grid->title('商品名')->display(function ($title) {
			$url = route('isbn.index') . '?isbn13=' . $this->isbn;
			return "<a target='_blank'  href='" . $url . "'>" . $title . "</a>";
		});
		$grid->img_url('图片')->image(100, 100)->modal(function ($modal) {
			$big_img = str_replace("_s", "", $this->img_url);
			return "<img style='width:100%;' src='" . $big_img . "'>";
		});
		$grid->num('销量');
		$grid->add_time('上架时间');
		$grid->price('销售价格');
		$grid->date('销售时间');
		$grid->areaname('销售地区');
		$grid->pinxiang('品相');
		$grid->shop_id('店铺ID')->display(function ($shop_id) {
			return "<a target='_blank'  href='http://shop.kongfz.com/" . $shop_id . "'>" . $shop_id . "</a>";
		});
		$grid->cloumn('多抓鱼搜索')->display(function () {
			return "https://www.duozhuayu.com/search/" . $this->isbn;
		})->link();
		$states = [
			'on' => ['value' => 1, 'text' => '打开', 'color' => 'primary'],
			'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
		];
		$grid->column('star')->switch();
		$grid->filter(function ($filter) {

			$filter->disableIdFilter();
			$filter->equal('isbn', 'ISBN');
			$filter->like('title', '商品名');
			$filter->equal('shop_id', '店铺ID');
			$yesterday = Carbon::yesterday()->toDateString();
			$filter->scope('yesterday', '昨日')->whereDate('date', $yesterday);
			// $filter->scope('yesterday', '本周')->whereDate('date', $yesterday);
			// $filter->scope('yesterday', '本月')->whereDate('date', $yesterday);

		});

		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	protected function detail($id) {
		$show = new Show(Goods::findOrFail($id));

		$show->id('ID');
		$show->created_at('Created at');
		$show->updated_at('Updated at');

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form(new Goods);
		$form->tools(function ($tools) {
			$tools->disableDelete();
		});
		$form->display('id');
		$form->text('title', '商品名');
		// $form->radio('star', '收藏')->options(['0' => '未收藏', '1' => '已收藏']);
		$states = [
			'on' => ['value' => 1, 'text' => '打开', 'color' => 'success'],
			'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
		];

		$form->switch('star', '收藏')->states($states);
		return $form;
	}
}
